package com.syf.core.log.trace;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import cn.hutool.core.util.StrUtil;

public class SyfTraceIdConvert extends ClassicConverter {
    private static ThreadLocal<String> logTraceIdLocal = new ThreadLocal<>();

    public static void setTraceId(String traceId) {
        logTraceIdLocal.set(traceId);
    }

    public static String getTranceId() {
        return logTraceIdLocal.get();

    }

    public static void clear() {
        logTraceIdLocal.remove();
    }

    @Override
    public String convert(ILoggingEvent iLoggingEvent) {
        return StrUtil.isBlank(SyfTraceIdConvert.getTranceId()) ? "N/A" : SyfTraceIdConvert.getTranceId();
    }
}
